<template>
    <div class="app-container">
        <el-form :inline="true" class="demo-form-inline">
               <el-form-item>
                    <el-input  v-model="searchObj.hosname" placeholder="医院名称" />
                </el-form-item>
               <el-form-item>
                    <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
                </el-form-item>
               <el-button type="primary" icon="el-icon-search" @click="getHospSetList()">查询</el-button>
            <el-button style="float: right;margin-right: 500px;" type="danger" size="large" @click="removeRows()">批量删除</el-button>
        </el-form>
        <!-- 列表 -->
        <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"/>
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column prop="hosname" label="医院名称" />
            <el-table-column prop="hoscode" label="医院编号" />
            <el-table-column prop="apiUrl" label="api基础路径" width="200" />
            <el-table-column prop="contactsName" label="联系人姓名" />
            <el-table-column prop="contactsPhone" label="联系人手机" />
            <el-table-column label="状态" width="80">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.status === 1">可用</el-tag>
                    <el-tag type="danger" v-if="scope.row.status === 0">不可用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="280" align="center">
                   <template slot-scope="scope">
                      <router-link :to="'/hospSet/edit/'+scope.row.id">
                         <el-button type="primary" size="mini" 
                             icon="el-icon-edit"> 
                         </el-button>
                      </router-link>
                      <el-button type="danger" size="mini" 
                         icon="el-icon-delete" @click="removeDataById(scope.row.id)"> 
                      </el-button>
                      <el-button v-if='scope.row.status===1' type="primary" size="mini" 
                          @click="lockHospSet(scope.row.id,0)"> 
                        锁定
                      </el-button>
                      <el-button v-if='scope.row.status===0' type="primary" size="mini" 
                          @click="lockHospSet(scope.row.id,1)">
                        解锁
                      </el-button>
                   </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
            :page-sizes="[5, 10, 15, 20]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</template>

<script>
    import { getHospSetList,deleteHospById,batchDeleteHospSet,lockHospitalSet } from '@/api/hospSet'
    export default {
        data() {
            return {
                page: 1,
                limit: 5,
                searchObj: {},
                list: [], //数据
                total: 0, //总记录数
                multipleSelection: []
            }
        },
        created() {
            this.getHospSetList()
        },
        methods: {
            getHospSetList() {
                getHospSetList(this.page, this.limit, this.searchObj).then(res => {
                    this.list = res.data.records
                    this.total = res.data.total
                }).catch(err => {
                    this.$message({
                        type: 'error',
                        message: '失败'
                    })
                })
            },
            //删除医院设置
            removeDataById(id){
                this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                    //调用方法进行删除
                    deleteHospById(id)
                        .then((res) => {
                        this.getHospSetList()
                        this.$message({
                            type: "success",
                            message: "删除成功",
                        });
                        })
                        .catch((err) => {
                        console.log(err);
                        this.$message({
                            type: "error",
                            message: "删除失败",
                        });
                        });
                    })
                    .catch(() => {
                    //判断点击取消
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
            },
            //用于获取复选框的值
            handleSelectionChange(selection){
                this.multipleSelection = selection
            },
            //批量删除
            removeRows(){
                if(this.multipleSelection==""){
                    this.$message({
                        type: "info",
                        message: "请先选择要删除的内容",
                    });
                    return;
                }
                this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                    //调用方法进行删除
                    var arr=[]
                    for(var i=0;i<this.multipleSelection.length;i++){
                        var id = this.multipleSelection[i].id
                        arr.push(id)
                    }
                    batchDeleteHospSet(arr)
                        .then((res) => {
                        this.getHospSetList()
                        this.$message({
                            type: "success",
                            message: "删除成功",
                        });
                        })
                        .catch((err) => {
                        console.log(err);
                        this.$message({
                            type: "error",
                            message: "删除失败",
                        });
                        });
                    })
                    .catch(() => {
                    //判断点击取消
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
            },
            //医院锁定与解锁
            lockHospSet(id,status){
                lockHospitalSet(id,status).then(res=>{
                    this.getHospSetList()
                        this.$message({
                            type: "success",
                            message: "设置成功",
                        });
                })
            },
            handleSizeChange(val) {
                this.limit = val;
                this.getHospSetList();
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getHospSetList();
            },
        }
    }
</script>